/*
 * OpenURL.java
 *
 * Created on November 22, 2005, 4:45 AM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package org.wiztools.te.plugin;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.wiztools.te.TEMod;
import org.wiztools.te.TEProcess;

/**
 *
 * @author subhash
 */
public class OpenURL implements TEProcess{
    
    private TEMod tem;
    private Container ct;
    private JTextField jtf;
    private JButton jb_ok;
    private JButton jb_cancel;
    
    public void setTEMod(TEMod tem){
        this.tem = tem;
    }
    
    public void init(){
        ct = new JPanel();
        ct.setLayout(new BorderLayout());
        
        JLabel jl_title = new JLabel("Open URL");
        jl_title.setHorizontalAlignment(JLabel.CENTER);
        ct.add(jl_title, BorderLayout.NORTH);
        
        JPanel jp_center = new JPanel();
        jp_center.setLayout(new BorderLayout());
        jtf = new JTextField(35);
        jp_center.add(jtf, BorderLayout.CENTER);
        jp_center.add(new JLabel("URL: "), BorderLayout.WEST);
        ct.add(jp_center, BorderLayout.CENTER);
        
        JPanel jp_south = new JPanel();
        jp_south.setLayout(new FlowLayout());
        jb_ok = new JButton("  Ok  ");
        jb_ok.setMnemonic('O');
        jb_ok.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae){
                try{
                    String str = getURLContent();
                    tem.newFile();
                    tem.setText(str);
                    tem.closeDialog();
                }
                catch(InvalidURLException iue){
                    JOptionPane.showMessageDialog(tem.getJD(), iue.getMessage(), "Error!", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        jp_south.add(jb_ok);
        jb_cancel = new JButton("Cancel");
        jb_cancel.setMnemonic('C');
        jb_cancel.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae){
                tem.closeDialog();
                tem.requestFocus();
            }
        });
        jp_south.add(jb_cancel);
        ct.add(jp_south, BorderLayout.SOUTH);
    }
    
    private String getURLContent() throws InvalidURLException{
        String str = jtf.getText();
        try{
            if(str == null || "".equals(str.trim())){
                throw new InvalidURLException("Null text found!");
            }
            URL url = new URL(str);
            Object obj = url.getContent();
            if(obj instanceof InputStream){
                BufferedReader br = new BufferedReader(new InputStreamReader((InputStream)obj));
                String str_tmp = null;
                StringBuffer sb = new StringBuffer();
                while((str_tmp=br.readLine())!=null){
                    sb.append(str_tmp).append("\n");
                }
                br.close();
                sb.deleteCharAt(sb.length()-1);
                str = sb.toString();
            }
        }
        catch(MalformedURLException mue){
            throw new InvalidURLException(mue.getMessage(), mue);
        }
        catch(IOException ioe){
            throw new InvalidURLException(ioe.getMessage(), ioe);
        }
        return str;
    }
    
    public void action(){
        tem.dispDialog(ct, "OpenURL");
    }
    
    public void destroy(){
        // do nothing!
    }
    
    class InvalidURLException extends Exception{
        public InvalidURLException(String msg, Throwable thr){
            super(msg, thr);
        }
        
        public InvalidURLException(String msg){
            super(msg);
        }
    }
}
